<?php include_once('header.php'); ?>
<?php include_once('left_navigation.php'); ?>
<?php
	include('../classess/add_ebook.php');

	$new_course = new add_course();

	if(isset($_POST['ebook_id']) && trim($_POST['ebook_id'])!=''){
		$add_update_course = $new_course->update_ebook_commission(); 
	}	
	
	if(isset($_GET['id'])){
		$get_single_course = $new_course->get_ebook($_GET['id']); 
	}

?>
<div class="col-lg-12"><!-- Start Middle Content Div-->
	<div class="box">
		
		<?php 
			if(isset($_GET['id'])){
		?>
		<div class="row">	
			<div class="col-lg-12"><!-- Start Middle Content Div-->
				<div class="box">
					
					<div class="box-header" data-original-title=""><!-- Start Box Header Div-->
						<h2><i class="fa fa-edit"></i><span class="break"></span><?php echo 'Edit ';?> Course</h2>
						<div class="box-icon">
							<a href="gallery.html#" class="btn-minimize"><i class="fa fa-chevron-up"></i></a>
							<a href="gallery.html#" class="btn-close"><i class="fa fa-times"></i></a>
						</div>
					</div><!-- End Box Header Div-->
					<div class="box-content"><!-- Start Box Content Div-->
						<div class="row">
							
							<div class="col-sm-12">
								<?php 
									if(isset($add_update_course)){	
								?>
								<div class="alert alert-info">
									<div style="float: right; width: 5%;"><button type="button" class="close" data-dismiss="alert">&times;</button></div>
									<div style="float:left;width: 92%;word-wrap: break-word;"><?php echo $add_update_course; ?></div>
									<div style="clear:both;"></div>							
								</div>
								<?php
									}
								?>
								<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>?ebk_comm=1<?php if(isset($_GET['id'])){ echo '&id='.$_GET['id']; } ?>" name="add_course_form" id="add_course_form" enctype="multipart/form-data">
									<div class="main_form_box col-sm-6">
										<div class="form-content  form-content-form">								
											
											<label>Course Name</label>
											<input type="text" placeholder="Ebook Name" name="ebook_name" id="ebook_name" value="<?php if(isset($get_single_course['0']->ebook_name)){ echo $get_single_course['0']->ebook_name; } ?>" readonly>
										</div>
									</div>

									<div class="main_form_box col-sm-6" >
										<div class="form-content  form-content-form">

											<label>Course Commission</label>
											<input type="text" name="ebook_commission" id="ebook_commission" value="<?php if(isset($get_single_course['0']->ebook_commission)){ echo $get_single_course['0']->ebook_commission; } ?>" placeholder="Ebook Commission"> 
										</div>
										<div style="text-align:right;">
											<button type="button" class="btn btn-blue login_button" name="add_user" id="add_user"><?php if(isset($_GET['id'])){ echo 'Edit'; }else{echo 'Add'; }?></button> 
										</div>
									</div>
									<div style="clear:both;"></div>									
									
									<input type="hidden" name="ebook_id" id="ebook_id" value="<?php if(isset($_GET['id'])){ echo $_GET['id']; } ?>">
									
								</form>

							</div><!--/col-->
						</div><!-- End Row div -->
					</div><!-- End Box Content Div -->

				</div><!-- End Box Div-->
			</div><!-- End Middle Content Div-->
		</div><!--/row-->
		<?php
			}
		?>

		<div class="row">		
			<div class="col-lg-12">
				<div class="box">
					<div class="box-header" data-original-title>
						<h2><i class="fa fa-user"></i><span class="break"></span>Courses</h2>
						<div class="box-icon">
							<a href="table.html#" class="btn-minimize"><i class="fa fa-chevron-up"></i></a>
							<a href="table.html#" class="btn-close"><i class="fa fa-times"></i></a>
						</div>
					</div>
					<div class="box-content">
						<?php 
							if(isset($courses)){	
						?>
						<div class="alert alert-info">
							<div style="float: right; width: 5%;"><button type="button" class="close" data-dismiss="alert">&times;</button></div>
							<div style="float:left;width: 92%;word-wrap: break-word;"><?php echo $delete_activate_user; ?></div>
							<div style="clear:both;"></div>							
						</div>
						<?php
							}
						?>
						<div class="search_box">
							<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>?ebk_comm=1" id="search_form" name="search_form">
								<div class="fl_left" style="width:50%;">
									<div class="fl_left sel_label_search" style="width:11%;">Search:</div>
									<div class="fl_left" style="width:86%;">	
										<input type="text" name="search_value" id="search_value" placeholder="Search" value="<?php if(isset($_POST['search_value'])){echo $_POST['search_value']; }else{ echo '';}  if(isset($_GET['srch_value'])){echo $_GET['srch_value']; }else{ echo '';}?>" style="width:45%;">&nbsp;
										<input type="submit" name="submit_search" id="submit_search" class="btn btn-blue srch_btn" value="Search">										
									</div>
									<div class="clear_fix"></div>
								</div>
								<div class="fl_right">
									<div class="fl_left" style="width:15%; margin-top:5px;">Number Of Records : </div>
									<div class="fl_left">
									<?php
										$limit_10='';
										$limit_25='';
										$limit_50='';
										$limit_100='';
										if(isset($_POST['sel_limit'])){
											if($_POST['sel_limit'] == '10'){
												$limit_10 = "selected='selected'";
											}
											if($_POST['sel_limit'] == '25'){
												$limit_25 = "selected='selected'";
											}
											if($_POST['sel_limit'] == '50'){
												$limit_50 = "selected='selected'";
											}
											if($_POST['sel_limit'] == '100'){
												$limit_100 = "selected='selected'";
											}
										}
										if(isset($_GET['sel_get_limit'])){
											if($_GET['sel_get_limit'] == '10'){
												$limit_10 = "selected='selected'";
											}
											if($_GET['sel_get_limit'] == '25'){
												$limit_25 = "selected='selected'";
											}
											if($_GET['sel_get_limit'] == '50'){
												$limit_50 = "selected='selected'";
											}
											if($_GET['sel_get_limit'] == '100'){
												$limit_100 = "selected='selected'";
											}
										}
									?>
									<select name="sel_limit" id="sel_limit" onchange="javascript:document.search_form.submit();">
										<!--<option value="10" <?php echo $limit_10; ?> >10</option>-->
										<option value="25" <?php echo $limit_25; ?> >25</option>
										<option value="50" <?php echo $limit_50; ?> >50</option>
										<option value="100" <?php echo $limit_100; ?> >100</option>
									</select>
										
									</div>
									<div class="clear_fix"></div>
								</div>
								<div class="clear_fix"></div>							
							</form>
						</div>
						<table class="table table-striped table-bordered">
						  <thead>
							  <tr>
								  <th>Ebook Name</th>
								  <th>Ebook Fee</th>
								  <th>Ebook Description</th>
								  <th>Ebook Commission</th>
								  <th>Is Active</th>
								  <th>Actions</th>
							  </tr>
						  </thead>   
						  <tbody>
							<?php
								$rec_limit = 25;
								if(isset($_POST['sel_limit'])){
									$rec_limit = $_POST['sel_limit'];
								}

								if(isset($_GET['sel_get_limit'])){
									$rec_limit = $_GET['sel_get_limit'];
								}

								$condition_search = '';
								$value = '';
								if(isset($_POST['search_value'])){										
									$value = trim($_POST['search_value']);
									
									if(!empty($value)){
										$cond_where = ' WHERE';
										$condition_search = $cond_where." (ebook_name like '%".$value."%' OR ebook_description like '%".$value."%' OR ebook_fee like '%".$value."%')";
									}
								}

								if(isset($_GET['srch_value'])){	
									$value = $_GET['srch_value'];
									
									if(!empty($value)){
										$cond_where = ' WHERE';

										$condition_search = $cond_where." (ebook_name like '%".$value."%' OR ebook_description like '%".$value."%' OR ebook_fee like '%".$value."%')";
									}
								}

								$select_count_course = "SELECT count(id) FROM tbl_ebook".$condition_search;
								$result_count_course = mysql_query($select_count_course);

								$row = mysql_fetch_array($result_count_course);
								$rec_count = $row[0];

								if( isset($_GET['page']) )
								{
								   $page = $_GET['page'] + 1;
								   $offset = $rec_limit * $page ;
								}
								else
								{
								   $page = 0;
								   $offset = 0;
								}

								$serial_number = $page*$rec_limit;
								
								$left_rec = $rec_count - ($page * $rec_limit);
								
								$select_course = "select * from  tbl_ebook".$condition_search." ORDER BY id DESC LIMIT ".$offset.", ".$rec_limit;
								$query_course = mysql_query($select_course) or die(mysql_error());
								
								$i = 1;
								if(mysql_num_rows($query_course)>0){
									while($row = mysql_fetch_array($query_course)){
										$serial_number++;
										echo '
											<tr>
												<td>'.$row['ebook_name'].'</td>
												<td>'.$row['ebook_fee'].'</td>
												<td>'.$row['ebook_description'].'</td>
												<td>';
														if($row['ebook_commission']){
															echo $row['ebook_commission'].'%';
														}else{
															echo 'No Commission';
														}													
													echo '</td>
												<td>';
													if($row['ebook_is_active']){
														echo '<span class="label label-success">Active</span>';
													}else{
														echo '<span class="label label-default">Inactive</span>';
													}
												echo '</td>
												<td>													
													<a class="btn btn-info" href="'.$_SERVER['PHP_SELF'].'?ebk_comm=1&id='.$row['id'].'" title="Edit">
														<i class="fa fa-edit "></i>  
													</a>
												</td>
											</tr>
										';
										$i++;
									}
								}else{
									echo '<tr>
										<td colspan="9" style="text-align:center;">No Records Found</td>
									</tr>';
								}

							?>						
						  </tbody>
					  </table>  
					  <?php		
						echo '<ul class="pagination">';
						if($rec_count>$rec_limit){
							if( $page > 0 && $left_rec > $rec_limit)
							{
							   $last = $page - 2;
							   echo '<li><a href="'.$_SERVER["PHP_SELF"].'?ebk_comm=1&page='.$last.'&srch_value='.$value.'&sel_get_limit='.$rec_limit.'" class="anc_click">Prev</a></li> ';
							   echo '<li><a href="'.$_SERVER["PHP_SELF"].'?ebk_comm=1&page='.$page.'&srch_value='.$value.'&sel_get_limit='.$rec_limit.'" class="anc_click">Next</a></li>';
							}
							else if( $page == 0 )
							{
							   echo '<li><a href="'.$_SERVER["PHP_SELF"].'?ebk_comm=1&page='.$page.'&srch_value='.$value.'&sel_get_limit='.$rec_limit.'" class="anc_click">Next</a></li>';
							}
							else if( $left_rec < $rec_limit || $left_rec == $rec_limit )
							{
							   $last = $page - 2;
							   echo '<li><a href="'.$_SERVER["PHP_SELF"].'?ebk_comm=1&page='.$last.'&srch_value='.$value.'&sel_get_limit='.$rec_limit.'" class="anc_click">Prev</a></li>';
							}
						}
						echo '</ul>';
					?>
				</div>
			</div><!--/col-->

		</div><!--/row-->
	</div><!-- End Box Div-->
</div><!-- End Middle Content Div-->
<?php include_once('footer.php'); ?>
<script>
	$ = jQuery.noConflict();
	$(document).ready(function(){	
		$('.close').click(function(){
			$('.alert-info').addClass('hide');
		});

		$('#add_user').click(function(){
			document.add_course_form.submit();
		});

		$('#submit_course_search').click(function(){
			document.search_course_form.submit()
		});
	});
		
	
</script>